﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net.Mail;
using System.Text;
using Rodunu.Utilities.Email.Properties;

namespace Rodunu.Utilities.Email
{
    public class Mailer
    {
        private SmtpClient _client;
        private readonly MessageFactory _messageFactory;

        public Mailer()
        {
            _messageFactory = new MessageFactory();
        }

        /// <summary>
        /// Send an email to the receiver in the toAddressList.
        /// </summary>
        /// <param name="toAddressList">List of receivers of the message</param>
        /// <param name="subject">Subject of the email</param>
        /// <param name="body">Body of the email</param>
        public MessageReturnCodes.Send SendEmail(Collection<string> toAddressList, string subject, string body)
        {
            if (!Settings.Default.SendEmail)
            {
                return MessageReturnCodes.Send.PreventedByConfiguration;
            }

            try
            {
                MailMessage msg = _messageFactory.BuildMessage(toAddressList, subject, body);
                Send(msg);
                msg.Dispose();
                return MessageReturnCodes.Send.Success;
            }
            catch (Exception)
            {
                return MessageReturnCodes.Send.Failed;
            }
        }

        /// <summary>
        /// Send an email to the receiver in the toAddress.
        /// </summary>
        /// <param name="toAddress">Receiver of the message</param>
        /// <param name="subject">Subject of the email</param>
        /// <param name="body">Body of the email</param>
        public MessageReturnCodes.Send SendEmail(string toAddress, string subject, string body)
        {
            if (!Settings.Default.SendEmail)
            {
                return MessageReturnCodes.Send.PreventedByConfiguration;
            }
            
            try
            {
                MailMessage msg = _messageFactory.BuildMessage(toAddress, subject, body);
                Send(msg);
                msg.Dispose();
                return MessageReturnCodes.Send.Success;
            }
            catch (Exception)
            {
                return MessageReturnCodes.Send.Failed;
            }
        }

        private void Send(MailMessage message)
        {
            _client = new SmtpClient(Settings.Default.SmtpServerAddress, Convert.ToInt32(Settings.Default.SmtpServerPort));

            // HACK:  put in user control security mechanism
            string username = Settings.Default.DefaultEmailAccountUserName;
            string password = Settings.Default.DefaultEmailAccountPassword;
            _client.Credentials = new System.Net.NetworkCredential(username, password);

            //client.UseDefaultCredentials = true;
            _client.Send(message);
        }
    }
}
